package com.rocket.database;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DataBaseOpenHelper extends SQLiteOpenHelper {

	static final String					DATABASE_NAME		= "rocket.db";

	static final int					DATABASE_VERSION	= 1;

	public static SQLiteDatabase		mDataBase			= null;

	private static DataBaseOpenHelper	instance			= null;

	public static void initDataBaseContext(Context aContext) {
		if (instance == null) {
			instance = new DataBaseOpenHelper(aContext, DATABASE_NAME, null, DATABASE_VERSION);
		}
		if (mDataBase == null) {
			mDataBase = instance.getWritableDatabase();
		}
		SharedPreferences share = aContext.getSharedPreferences("database", 0);
		boolean isInit = share.getBoolean("init", false);
		if(!isInit){
			Missile83Sql.getInstance().init();
			MissileJD89Sql.getInstance().init();
			RocketBL1Sql.getInstance().init();
			RocketHJD82Sql.getInstance().init();
			RocketRYI6300Sql.getInstance().init();
			RocketWR98Sql.getInstance().init();
			WorkSql.getInstance().init();
			WorkStationSql.getInstance().init();
			share.edit().putBoolean("init", true).commit();
		}
	}

	public static synchronized DataBaseOpenHelper getInstance() {
		return instance;
	}

	private DataBaseOpenHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		
	}

	@Override
	protected void finalize() throws Throwable {
		close();
		super.finalize();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public void close() {
		try {
			if (mDataBase != null) {
				mDataBase.close();
				mDataBase = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
